package wendyeq.iweb.blog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import wendyeq.iweb.blog.dao.IArticleDAO;
import wendyeq.iweb.blog.dao.ICategoryDAO;
import wendyeq.iweb.blog.pojo.Article;
import wendyeq.iweb.blog.pojo.Category;
import wendyeq.iweb.blog.service.ICategoryService;

@Service("categoryServiceImpl")
public class CategoryServiceImpl implements ICategoryService{
	private static final Logger logger = Logger
			.getLogger(CategoryServiceImpl.class);

	@Resource(name="categoryDAO")
	private ICategoryDAO categoryDAO;
	
	@Resource(name="articleDAO")
	private IArticleDAO articleDAO;
	
	@Override
	public boolean save(Category entity) {
		if(this.categoryDAO.findByName(entity.getName())==null) {
			this.categoryDAO.saveOrUpdate(entity);
			logger.warn("CategoryService -- do insert!");
		} else{
			logger.warn("CategoryService -- do insert false!");
		}
		return false;
	}

	@Override
	public boolean delete(String id) {
		Category cat = this.categoryDAO.findById(id);
		if(this.categoryDAO.findByName("Default")==null){
			logger.warn("do new default");
			this.categoryDAO.saveOrUpdate(new Category("Default"));
		}
		List<Article> articles = this.articleDAO.findByCat(0,100000000,cat.getName());
		Category category = this.categoryDAO.findByName("Default");
		for(Article a : articles){
			a.setCategory("Default");
			this.articleDAO.saveOrUpdate(a);
			if(category!=null){
				category.setCount(category.getCount()+1);
			}
		}
		this.categoryDAO.saveOrUpdate(category);
		return this.categoryDAO.delete(id);
	}

	@Override
	public boolean modify(String oldName, String newName) {
		List<Article> articles = this.articleDAO.findByCat(0,1000000,oldName);
		for(Article a : articles){
			a.setCategory(newName);
			this.articleDAO.saveOrUpdate(a);
		}
		Category category = this.categoryDAO.findByName(oldName);
		category.setName(newName);
		logger.warn("newName"+category.getName());
		return this.categoryDAO.saveOrUpdate(category);
		
	}

	@Override
	public Category findById(String id) {
		return this.categoryDAO.findById(id);
	}

	@Override
	public List<Category> findByPage(int min, int max) {
		return this.categoryDAO.findByPage(min, max);
	}

	@Override
	public List<Category> findAll() {
		return this.categoryDAO.findAll();
	}

	
//	public void update(String oldName, String newName) {
//		this.articleService.updateCategory(oldName, newName);
//		Category category = this.categoryDAO.findByName(oldName);
//		category.setName(newName);
//		logger.warn("newName"+category.getName());
//		this.categoryDAO.saveOrUpdate(category);
//	}
//	
//	public void delete(String name) {
//		Category cat = this.categoryDAO.findByName(name);
//		this.articleService.updateByCategory(cat.getName());
//		this.categoryDAO.delete(name);
//	}
//
//	public List<Category> findAll() {
//		return this.categoryDAO.findAll();
//	}
//
//	public List<Category> findByPage(int min, int max) {
//		return this.categoryDAO.findByPage(min, max);
//	}

	
}
